import Category_list from '../models/classify_list'
import Classify_types from '../models/classify_types'
import http from "../prototype/http";


class Read_pare360_data extends http {
    constructor() {
        super();
        this.classify_type = [
            {key: 'main', name: '首页', is_children: false},
            {key: 'serial', name: '电视剧', is_children: true},
            {key: 'film', name: '电影', is_children: true},
            {key: 'variety', name: '综艺', is_children: true},
            {key: 'cartoon', name: '动漫世界', is_children: true},
        ]

        this.on_read = this.on_read.bind(this);
        this.on_init_classify = this.on_init_classify.bind(this);
        this.on_init_types = this.on_init_types.bind(this);
    }

    on_read() {
        console.log(this);
        this.on_init_classify();
        this.on_init_types();
    }

    /**
     * 首页 - 电视剧 - 电影 -  综艺 - 卡通 （列表）
     * @param key
     * @param is_children
     * @param name
     */
    on_init_classify(key, is_children, name) {
        this.classify_type.forEach(async item => {
            if (key !== undefined && name !== undefined && is_children !== undefined) {
                if (key !== item.key || name !== item.name || is_children !== item.is_children) {
                    return;
                }
            }

            const res = await this.fetch(item.key);
            if (res.status === 200) {
                const {lists, banner} = res.data || {};
                await Category_list.deleteMany({classify: item.key})
                await Category_list.create({
                    classify: item.key,
                    name: item.name,
                    is_children: item.is_children,
                    lists: lists || [],
                    banner: banner || []
                });
            } else {
                console.log(res, 'on_init_classify', (item.key), this.baseURL);
            }

            // 执行全部 还是全部
        })
    }

    /**
     * 首页 - 电视剧 - 电影 -  综艺 - 卡通 （筛查）
     * @param key
     * @param is_children
     * @param name
     */
    on_init_types(key, is_children, name) {
        this.classify_type.forEach(async item => {
            if (key !== undefined && name !== undefined && is_children !== undefined) {
                if (key !== item.key || name !== item.name || is_children !== item.is_children) {
                    return;
                }
            }

            if (item.is_children) {
                const res = await this.fetch('category', {category: item.key});
                if (res.status === 200) {
                    const {rank, type, year, area, act} = res.data || {};
                    await Classify_types.deleteMany({classify: item.key})
                    await Classify_types.create({
                        classify: item.key,
                        name: item.name,
                        is_children: item.is_children,
                        rank: rank || [],
                        type: type || [],
                        year: year || [],
                        area: area || [],
                        act: act || [],
                    });
                } else {
                    console.log(res ,' on_init_types', (item.key));
                }
            }

        })
    }
}


export default Read_pare360_data;
